﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Base;
using Corporation.News;

public partial class Member_News_NewsList : nspBase.MemberBasePge
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckCookies())
        {
            Response.Redirect("../../login/login.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
    }
    private void Bind()
    {
        btnDelete.Attributes.Add("onclick", "return confirm('你确认要删除吗？')");
        int userid = int.Parse(GetCookie("UserID"));
        string strWhere = "C_news.UserID=" + userid;
        string Title = txtTitle.Text.Trim();
        string CompanyName = txtCompany.Text.Trim();
        if (Title != "")
            strWhere += " and Title like '%" + Title + "%'";
        if (CompanyName != "")
            strWhere += " and CompanyName like '%" + CompanyName + "%'";
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx";
        PageNo1.Strurl = strURl;
        if (Request.QueryString["page"] != null)
        {
            PageNo1.Pageindex = Convert.ToInt32(Request.QueryString["page"].ToString());
            PageNo1.PageSize = 12;
            if (Session["strWhere"] != null)
            {
                PageNo1.Where = Session["strWhere"].ToString();
            }
            else
            {
                PageNo1.Where = strWhere;
            }
            PageNo1.Table = "C_news INNER JOIN U_UserInfo ON C_news.UserID =U_UserInfo.UserID";
            PageNo1.FldName = "AddDate";
            PageNo1.StrGetFields = "C_news.*,U_UserInfo.CompanyName";
            DataTable table = PageNo1.BindViewDesc();
            grdCorpNews.DataSource = table.DefaultView;
            grdCorpNews.DataBind();
        }
        else
        {
            PageNo1.Pageindex = 1;
            PageNo1.PageSize = 12;
            PageNo1.Where = strWhere;
            Session["strWhere"] = strWhere;
            PageNo1.Table = "C_news INNER JOIN U_UserInfo ON C_news.UserID =U_UserInfo.UserID";
            PageNo1.FldName = "AddDate";
            PageNo1.StrGetFields = "C_news.*,U_UserInfo.CompanyName";
            DataTable table = PageNo1.BindViewDesc();
            grdCorpNews.DataSource = table.DefaultView;
            grdCorpNews.DataBind();
        }
    }
    public string GetShortDate(string dateStr)
    {
        if (dateStr != string.Empty)
            return Convert.ToDateTime(dateStr).ToShortDateString();
        else
            return string.Empty;
    }
    protected void grdCorpNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCorpNews.PageIndex = e.NewPageIndex;
        Bind();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < grdCorpNews.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)grdCorpNews.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                int id = Convert.ToInt32(grdCorpNews.DataKeys[i].Value);
                INews RNew = new RNews();
                RNew.Delete(id);
            }
        }
        Bind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strWhere = " C_news.UserID=" + Base.BasePage.GetCookie("UserID");
        string Title = txtTitle.Text.Trim();
        string CompanyName = txtCompany.Text.Trim();
        if (Title != "")
        {
            Title = Base.GetRequest.Filter(Title);
            strWhere += " and Title like '%" + Title + "%'";
        }
        if (CompanyName != "")
        {
            CompanyName = Base.GetRequest.Filter(CompanyName);
            strWhere += " and CompanyName like '%" + CompanyName + "%'";
        }
        Session["strWhere"] = strWhere;
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx?page=1";
        Response.Redirect(strURl);
        Response.End();
    }
}
